// pages/new-stadium-page/new-stadium-page.js
Page({
    data: {
        //图片
        showLoadingState:false,
        p1:"https://pic.imgdb.cn/item/62f4598b16f2c2beb185c3e8.png",
        p2:"https://pic.imgdb.cn/item/62da57a1f54cd3f9379984c0.png",
        p3:"https://pic.imgdb.cn/item/62da57a4f54cd3f937999196.png",
        p4:"https://pic.imgdb.cn/item/62da57a8f54cd3f93799a553.png",
        p5:"https://pic.imgdb.cn/item/62da57c1f54cd3f9379a3037.png",
        p16:"https://pic.imgdb.cn/item/62dfd88bf54cd3f937934a0d.png",
        //储存和展示的数据
        locationId:0,
        stadium:{
            stadium_name:"请输入场地名称",
            stadium_sports:1,
            stadium_price:10,
            stadium_state:0,
        },
        //数据转换
        sportsArray:['所有项目','足球','篮球','排球','羽毛球','乒乓球','棒球','网球'],
        location_state_array:['上架','下架'],
    },
    onLoad(options) {
        this.setData({
            "locationId":options.locationId,
        });
    },
    onChangeStadiumSports:function(e){
        var x = e.detail.value;
        this.setData({
            'stadium.stadium_sports':x,
        })
    },
    onChangeStadiumPrice:function(e){
        var that = this;
        wx.showModal({
          editable:true,
          title:"请输入该场地每小时的价格",
          type:Number,
          success(res){
              if(res.confirm){
                  var x = parseInt(res.content);
                  if(!isNaN(x)){
                  that.setData({
                      [`stadium.stadium_price`]:x,
                  })
                  }
                  else if(isNaN(x)){
                    wx.showModal({
                      content:"您的输入有误，请重试",
                      showCancel:false,
                    })
                  }
              }
          }
        })
    },
    onChangeStadiumState:function(e){
        this.data.stadium.stadium_state = (this.data.stadium.stadium_state+1)%2;
        this.setData({
            [`stadium.stadium_state`]:this.data.stadium.stadium_state,
        })
    },
    onChangeStadiumName:function(e){
        var that = this;
        wx.showModal({
            title:"请输入新场馆名字",
            editable:true,
            success(res){
                if(res.confirm){
                    that.setData({
                        "stadium.stadium_name":res.content,
                    })
                }
            }
        })
    },
    onConfirmAdd:function(e){
        //提交所有数据
        var that = this;
        that.onShowLoading();

        wx.request({
          url: getApp().globalData.serverUrl,
          method:"GET",
          timeout:10000,
          data:{
            //各种数据的传送
            operation:"CREATE_PLAYGROUND",
            name:that.data.stadium.stadium_name,
            sportsId:that.data.stadium.stadium_sports,
            state:that.data.stadium.stadium_state,
            stadiumId:that.data.locationId,
            price:that.data.stadium.stadium_price,
          },
          success(res){
            if(res.data == false){
                that.onHideLoading()
                wx.showModal({
                    showCancel:false,
                    title:"请求失败,请重试",
                });
            }
            else{
                //成功获取到信息后的操作
                that.onHideLoading()
                wx.navigateBack({

                })
            }
          },
          fail(res){
              that.onHideLoading();
            wx.showModal({
                showCancel:false,
                title:"请求超时,请重试",
            });
          },
        });
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
})